package com.www;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUI_8 {
    private static void createAndShowGUI(){
        JFrame f = new JFrame("MouseEvent");
        f.setLayout(new FlowLayout());//为窗口设置布局
        f.setSize(500,500);
        f.setLocation(500,200);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton but = new JButton("Button");
        f.add(but);
        but.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON1)
                {
                    System.out.println("鼠标单击事件");
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
                System.out.println("鼠标按下事件");
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                System.out.println("鼠标放开事件");
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.println("鼠标进入按钮区域事件");
            }

            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("鼠标移出按钮区域事件");
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(GUI_8::createAndShowGUI);
    }
}












